package com.baseproject.util;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.ServiceInfo;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Parcelable;
import android.text.TextUtils;
import android.util.Log;

import java.io.File;

/**
 * @version V1.0  2016/11/4上午10:36
 * @author:OliverTan(www.tandunzhao.cn)
 */
public class AppUtil {
  public static final String TAG = "AppUtil";

  public static String getAppChannel(Context context) {
    final String market = getMetaData(context, "UMENG_CHANNEL");
    if(TextUtils.isEmpty(market)) {
      return "default";
    } else {
      return market;
    }
  }

  /**
   * 获取应用程序图标
   *
   * @param context
   * @return
   */
  public static Drawable getAppIcon(Context context) {
    return getAppIcon(context, context.getPackageName());
  }

  /**
   * 获取应用程序图标
   *
   * @param context
   * @param packageName
   * @return
   */
  public static Drawable getAppIcon(Context context, String packageName) {
    try {
      return getApplicationInfo(context, packageName).loadIcon(context.getPackageManager());
    } catch (Exception e) {
      Log.e(TAG, "getAppIcon: ", e);
    }
    return null;
  }

  /**
   * 获取应用程序名称
   *
   * @param context
   * @return
   */
  public static String getAppName(Context context) {
    return getAppName(context, context.getPackageName());
  }

  /**
   * 获取应用程序名称
   *
   * @param context
   * @param packageName
   * @return
   */
  public static String getAppName(Context context, String packageName) {
    try {
      return getApplicationInfo(context, packageName).loadLabel(context.getPackageManager()).toString();
    } catch (Exception e) {
      Log.e(TAG, "getAppName: ", e);
    }
    return null;
  }

  /**
   * 获取到程序签名
   *
   * @param context
   * @return
   */
  public static String getAppSignature(Context context) {
    return getAppSignature(context, context.getPackageName());
  }

  /**
   * 获取到程序签名
   *
   * @param context
   * @param packageName
   * @return
   */
  public static String getAppSignature(Context context, String packageName) {
    try {
      PackageInfo packInfo = getPackageManager(context).getPackageInfo(packageName, PackageManager.GET_SIGNATURES);
      return packInfo.signatures[0].toCharsString();
    } catch (Exception e) {
      Log.e(TAG, "getAppSignature: ", e);
    }
    return null;
  }

  /**
   * 获取 AndroidManifest Service 中meta-data中的值
   *
   * @param context
   * @param clazz
   * @param name
   * @return
   */
  public static String getServiceMetaData(Context context, Class clazz, String name) {
    try {
      ServiceInfo ai = getPackageManager(context).getServiceInfo(getComponentName(context, clazz), PackageManager.GET_META_DATA);
      return ai.metaData.getString(name, null);
    } catch (Exception e) {
      Log.e(TAG, "getServiceMetaData: ", e);
    }
    return null;
  }

  /**
   * 获取 AndroidManifest Activity 中meta-data中的值
   *
   * @param context
   * @param clazz
   * @param name
   * @return
   */
  public static String getActivityMetaData(Context context, Class clazz, String name) {
    try {
      ActivityInfo ai = getPackageManager(context).getActivityInfo(getComponentName(context, clazz), PackageManager.GET_META_DATA);
      return ai.metaData.getString(name, null);
    } catch (Exception e) {
      Log.e(TAG, "getActivityMetaData: ", e);
    }
    return null;
  }

  public static ComponentName getComponentName(Context context, Class clazz) {
    return new ComponentName(context, clazz);
  }

  /**
   * 获取AndroidManifest application 中meta-data中的值
   *
   * @param context
   * @param name
   * @return
   */
  public static String getMetaData(Context context, String name) {
    try {
      ApplicationInfo ai = getPackageManager(context).getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA);
      return ai.metaData.getString(name, null);
    } catch (Exception e) {
      Log.e(TAG, "getMetaData: ", e);
    }
    return null;
  }

  /**
   * 获取APP 版本号
   *
   * @param context
   * @return
   */
  public static int getAppVersionCode(Context context) {
    return getAppVersionCode(context, context.getPackageName());
  }

  /**
   * 获取APP 版本号
   *
   * @param context
   * @param packageName
   * @return
   */
  public static int getAppVersionCode(Context context, String packageName) {
    try {
      return getPackageInfo(context, packageName).versionCode;
    } catch (Exception e) {
      Log.e(TAG, "getAppVersionCode: ", e);
    }
    return 0;
  }

  /**
   * 获取APP 版本名称
   *
   * @param context
   * @return
   */
  public static String getAppVersionName(Context context) {
    return getAppVersionName(context, context.getPackageName());
  }

  /**
   * 获取APP 版本名称
   *
   * @param context
   * @param packageName
   * @return
   */
  public static String getAppVersionName(Context context, String packageName) {
    try {
      return getPackageInfo(context, packageName).versionName;
    } catch (Exception e) {
      Log.e(TAG, "getAppVersionName: ", e);
    }
    return null;
  }

  public static PackageManager getPackageManager(Context context) {
    return context.getPackageManager();
  }

  public static ApplicationInfo getApplicationInfo(Context context, String packageName) throws Exception {
    return context.getPackageManager().getApplicationInfo(packageName, 0);
  }

  public static PackageInfo getPackageInfo(Context context, String packageName) throws Exception {
    return context.getPackageManager().getPackageInfo(packageName, 0);
  }

  public static void createShortCut(Activity act, int iconResId, int appNameResId) {
    Intent intent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
    // 不允许重复创建
    intent.putExtra("duplicate", false);
    // 需要现实的名称
    intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, act.getString(appNameResId));
    // 快捷投标
    Parcelable icon = Intent.ShortcutIconResource.fromContext(act.getApplicationContext(), iconResId);
    intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
    // 点击快捷图片，运行程序
    intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(act.getApplicationContext(), act.getClass()));
    // 发送广播
    act.sendBroadcast(intent);
  }

  public static void installApk(Context context, String path, String apkName) {
    Uri uri = Uri.fromFile(new File(path + File.separator + apkName));
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    intent.setDataAndType(uri, "application/vnd.android.package-archive");
    context.startActivity(intent);
  }
}
